package wens.pig.base.plugin.form;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.exception.KDBizException;
import kd.bos.form.FormShowParameter;
import kd.bos.form.events.PreOpenFormEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;

public class OrgFilter extends AbstractFormPlugin {
    String orgpatternName = "种猪场,扩繁场,原种场,区域公猪站,四级单位,三级单位,二级单位,温氏集团";

    @Override
    public void preOpenForm(PreOpenFormEventArgs e) {
        try {
            FormShowParameter formShowParameter = e.getFormShowParameter();
            if (null != formShowParameter) {
                String appId = formShowParameter.getAppId();
                if (StringUtils.isEmpty(appId) || appId.startsWith("wens_cost") || !appId.startsWith("wens_")) {
                    // appId为空 直接通过url访问，这时appId为空 http://127.0.0.1:8080/ierp/index.html?formId=xx&pkId=11111
                    // 不是二开的应用不用校验权限，以兼容从工作流那边进来的,或者是标准产品的上下查
                    // 成本核算的也不需要校验
                    return;
                }
            }

            this.checkOrg(e.getFormShowParameter());
            e.getFormShowParameter().addCustPlugin("wens.pig.base.plugin.form.AddBatchPlugin");
        } catch (KDBizException ex) {
            e.setCancelMessage(ex.getMessage());
            e.setCancel(true);
        }

    }

    private void checkOrg(FormShowParameter view) {
        Object isExtra = view.getCustomParam("isextra");
        DynamicObject orgData = BusinessDataServiceHelper.loadSingle(RequestContext.get().getOrgId(), "bos_org", "orgpattern");
        String name = orgData.getString("orgpattern.name");
        if (!orgpatternName.contains(name)) {
            throw new KDBizException("当前所选“业务单元”的『形态』必须为" + orgpatternName);
        }

    }
}
